package com.tengju.activity.infrastructure.repository;

import com.tengju.activity.domain.model.activity.ActivityConfig;
import com.tengju.activity.domain.model.activity.ActivityConfigRepository;
import com.tengju.activity.domain.model.activity.ActivityId;
import com.tengju.activity.infrastructure.persistence.mybatis.dataobject.ActivityConfigDO;
import com.tengju.activity.infrastructure.persistence.mybatis.mapper.ActivityConfigMapper;
import com.tengju.activity.infrastructure.repository.convert.ActivitySignUpConvert;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Optional;

@Repository
public class ActivityConfigRepositoryImpl implements ActivityConfigRepository {

    @Resource
    ActivityConfigMapper activityConfigMapper;

    @Override
    public ActivityId queryActivityId(String activityCode) {
        ActivityConfigDO activityConfigDO = activityConfigMapper.queryByActivityCode(activityCode);
        return Optional.ofNullable(activityConfigDO).map(ActivityConfigDO::getActivityId).map(ActivityId::new).orElse(null);
    }

    @Override
    public void store(ActivityConfig activityConfig) {
        ActivityConfigDO activityConfigDO = ActivitySignUpConvert.convertToDo(activityConfig);
        activityConfigMapper.store(activityConfigDO);
    }

    @Override
    public void changeActivityIdByCode(ActivityId activityId, String activityCode) {
        activityConfigMapper.changeActivityIdByCode(activityId.getValue(),activityCode);
    }
}
